package cn.school.controller.educational;

import cn.school.attendance.dormitory.vo.DormitoryAttendanceVo;
import cn.school.classManage.dao.ClassTeacherRelationDao;
import cn.school.classManage.service.SchoolClassService;
import cn.school.classManage.vo.ClassTeacherRelationVo;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.educational.entity.HomeWorkInfo;
import cn.school.educational.entity.HomeworkComments;
import cn.school.educational.service.HomeWorkInfoService;
import cn.school.student.service.StudentInfoService;
import cn.school.sys.entity.SysUser;
import cn.school.sys.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@Api(tags = "家庭作业")
@RequestMapping("/homeWork")
public class HomeWorkController {

    @Resource
    HomeWorkInfoService homeWorkInfoService;
    @Resource
    SysUserService sysUserService;
    @Resource
    ClassTeacherRelationDao classTeacherRelationDao;
    @Resource
    SchoolClassService schoolClassService;


    @ApiOperation("分页查询作业")
    @PostMapping("/pageList")
    public CommonResult<CommonPage<HomeWorkInfo>> pageList(@RequestBody HomeWorkInfo vo) {
        IPage<HomeWorkInfo> pageList = homeWorkInfoService.pageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("学生查询作业")
    @PostMapping("/studentPageList")
    public CommonResult<CommonPage<HomeWorkInfo>> studentPageList(@RequestBody HomeWorkInfo vo) {
        IPage<HomeWorkInfo> pageList = homeWorkInfoService.studentPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("新增or修改")
    @PostMapping("/addOrUpdateWork")
    public CommonResult<Integer> addOrUpdateWork(@RequestBody HomeWorkInfo vo) {
        int num = homeWorkInfoService.addOrUpdateHomeWork(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(("作业文件上传"))
    @PostMapping("/homeWorkFileUpload")
    @CrossOrigin
    public CommonResult<String> homeWorkFileUpload(MultipartFile file) throws IOException {
        String url = homeWorkInfoService.homeWorkFileUpload(file);
        return CommonResult.success(url);
    }

    @ApiOperation("根据ID删除")
    @PostMapping("/deleteById")
    public CommonResult<Integer> deleteById(@RequestBody HomeWorkInfo vo) {
        int num = homeWorkInfoService.deleteById(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据ID查询详情")
    @GetMapping("/details/{id}")
    public CommonResult<HomeWorkInfo> workDetails(@PathVariable("id")String id) {
        HomeWorkInfo info = homeWorkInfoService.workDetails(id);
        return CommonResult.success(info);
    }

    @ApiOperation("新增评论")
    @PostMapping("/addComments")
    public CommonResult<Integer> addComments(@RequestBody HomeworkComments comments) {
        int num = homeWorkInfoService.addComments(comments);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("新增回复")
    @PostMapping("/addReply")
    public CommonResult<Integer> addReply(@RequestBody HomeworkComments comments) {
        int num = homeWorkInfoService.addReply(comments);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询评论")
    @PostMapping("/findCommentsDetails")
    public CommonResult<List<HomeworkComments>> findCommentsDetails(@RequestBody HomeworkComments comments) {
        List<HomeworkComments> list = homeWorkInfoService.findCommentsDetails(comments.getHomeworkId());
        return CommonResult.success(list);
    }

    @ApiOperation("查询评论的回复")
    @PostMapping("/findreplyDetails")
    public CommonResult<List<HomeworkComments>> findreplyDetails(@RequestBody HomeworkComments comments) {
        List<HomeworkComments> list = homeWorkInfoService.findreplyDetails(comments.getPid());
        return CommonResult.success(list);
    }


    @GetMapping("/getTeacherClass")
    @ApiOperation("clockIn => 获取当前老师的班级")
    public CommonResult<List<ClassTeacherRelationVo>> getTeacherClass() {
        return CommonResult.success(classTeacherRelationDao.selectClassTeacher(SysUserUtils.getSysUser().getId()+""));
    }

    @GetMapping("/getTeacherSubj/{classId}")
    @ApiOperation("clockIn => 获取当前老师的班级")
    public CommonResult<List<ClassTeacherRelationVo>> getTeacherSubj(@PathVariable("classId")String classId) {
        return CommonResult.success(classTeacherRelationDao.selectTeacherSubj(SysUserUtils.getSysUser().getId()+"",classId));
    }

    @GetMapping("/getTeacherSubjAll/{classId}")
    @ApiOperation("clockIn => 获取当前老师的班级")
    public CommonResult<List<ClassTeacherRelationVo>> getTeacherSubjAll(@PathVariable("classId")String classId) {
        return CommonResult.success(classTeacherRelationDao.selectTeacherSubj(null,classId));
    }

    @ApiOperation("根据id删除作业和评论")
    @PostMapping("/deleteWithComments/{id}")
    public CommonResult<Integer> deleteWithComments(@PathVariable String id){
        return homeWorkInfoService.deleteWithComments(id) ? CommonResult.success() : CommonResult.failed("500");
    }

}
